What is Java?
Java is a programming language. The major benefits of Java include a set of powerful libraries and its portability. As a result of the portability, Java has been applied to the creation of Applets. These applets are small programs that run inside a browser, or web page. Applets allow a programmer to deploy full blown applications on the web, without the limitations of HTML for itÆs user interface.
As Java gains in usage, libraries are being written to support CORBA, database access and most of the other Client-Server technologies. So, why not replace HTML and CGI with Java. First, HTML is a great way to display information. Java applets, do not have access, currently, to the page layout capabilities of HTML. Although a Java applet can interact with the user better than an HTML page can, an HTML page is a better format for displaying reports, catalogs, and other text based information. Luckily, Java and HTML can work in harmony since Java applets can live in HTML pages.
Why use CGI with Java?
Although Java applets can use some client server technologies, applets have security restrictions placed on them. Usually, these restrictions include a limitation on network connections from the client to the web server. Java applets cannot normally connect to an arbitrary server machine. At first this seems like an unnecessary restriction. But keep in mind that applets are written by someone else and run on your machine. There is a huge security risk in allowing that program to run on your machine and connect to any other machine that it wants to. The same risks prevent applets from accessing the hard disk on the client machine.
If applets can only talk to the web server, database servers must be on the web server machine or there must to be a program that forwards database requests from the web server to the database server. This leads to the second issue in network programming with Java. Although many companies want to have applets on the WWW that talk to their database, most do not want to allow connections to their database server from the Internet. To prevent these connections, companies set up firewalls that only allow traffic between clients and the web server. With this firewall in place, the Java applet can only talk to the web server.
In this secure environment, the easiest way to provide database access for Java applets, in a server independent manner, is to use CGI. Java applets can connect to CGI scripts on the server, send them requests, and read the reply. This type of connection is all "web-based" which allows it to work anywhere that normal HTML browsing works. Since CGI scripts can access files, databases and other arbitrary services, Java with CGI is a secure, portable and powerful combination.
Keep in mind that by using CGI with applets you may be changing their normal mode of operation. Instead of form requests, scripts run by applets may receive arbitrary string requests. Similarly, most CGI scripts return HTML. But a script run by an applet will probably return information as text, in a format encoded for the applet to interpret. This is because the applet will display the information in its own interface, not as HTML. As an example, an applet might send a CGI script an SQL string, which the scripts executes on a database. The script could return data, that the applet stores in objects, and displays in text fields.
In the next two sections we will see how to access CGI scripts from a Java applet, and how to write CGI scripts in Java. A Cgi class for decoding and encoding CGI data is provided on the CD. This library is equivalent to the one created in Chapter one of the CGI How To.
Talking to CGI Scripts from Java Applets
Java applets can send and receive data from CGI scripts using the URLConnection object. This object is created by a URL object, using the openConnection method. Once you have a connection, you can open an output stream to send data to the script. After closing the output stream, open an input stream to read the CGI scripts reply.
The following example reads a URL from a textfield, and sends the provided data to that URL. After sending the data, it reads the reply data from the URL and appends it to a text area. The data being sent must be encoded before it is used. To encode data, either use the URLEncoder object, provided with the JDK, which encodes a single string, or use the Cgi class, from the CD, to encode an entire Hashtable.
public void sendData(String theURL,String actualData) { URL realURL; URLConnection connection; DataInputStream dataIn; PrintStream printOut; StringBuffer data = new StringBuffer(); String curLine; results.setText(""); try { realURL = new URL(theURL); connection = realURL.openConnection(); printOut = new PrintStream(connection.getOutputStream()); //Send the data printOut.println(actualData); //Close the print stream to insure data transfer printOut.close(); dataIn = new DataInputStream(connection.getInputStream()); do { curLine = dataIn.readLine(); if(curLine != null) { if(data.length() != 0) data.append('\n'); data.append(curLine); results.setText(data.toString()); } }while(curLine != null); results.setText(data.toString()); dataIn.close(); } catch(MalformedURLException exp) { results.setText("Malformed URL"); } catch(IOException exp) { results.setText("Error Reading from URL"); } }
Because Java is a powerful programming language, you may want to write your CGI scripts to Java. The main disadvantage of using Java will be the lack of PerlÆs string manipulation techniques, or CÆs many libraries. If you do decide to use Java for CGI scripts a Cgi class is provided on the CD that is equivalent to the CGI libraries for Perl and C discussed in Chapter 1.
The main issue in using Java for CGI revolves around setting up your environment. A sample CGI script is provided on the CD. This script is equivalent to the Chapter 1 section 10 example. In order to use Java, a shell script is made the actual target URL. This script runs the interpreter on the Java program. When the interpreter is run, the required environment variables must provided as command line arguments. These include REQUEST_METHOD, CONTENT_LENGTH and QUERY_STRING. If you need other environmental variables, you must add them to the shell script.